var app = getApp();

Page({
    onLoad: function(e) {
        app.setNavigationBarColor(this);
        this.getCollectList();
    },
    data: {
        loadmore: true,
        page: 1,
        collect_list: [],
        mygd: !1,
        jzgd: !0,
        isDel: -1,
        delBtnWidth: 180
    },
    // 获取收藏列表
    getCollectList: function() {
        var ths = this,
            user_id = wx.getStorageSync("users").id;
        app.util.request({
            url: "entry/wxapp/CollectList",
            cachetime: "0",
            data: {
                user_id: user_id,
                page: ths.data.page,
                pagesize: 10
            },
            success: function(res) {
                let list = ths.data.collect_list;
                if (ths.data.is_onPullDown) {
                    list = [];
                }
                list = list.concat(res.data.collect);
                res.data.collect.length < 10 ?
                    ths.setData({
                        mygd: !0,
                        jzgd: !0,
                        isjzz: !1,
                        collect_list: list,
                        loadmore: false,
                        is_onPullDown: false
                    }) :
                    ths.setData({
                        jzgd: !0,
                        isjzz: !1,
                        collect_list: list,
                        is_onPullDown: false
                    });
                ths.data.page++;
                wx.stopPullDownRefresh();
            }
        });
    },
    // 取消收藏
    DelCollect: function(t) {
        let ths = this,
            scid = t.currentTarget.dataset.scid;
        app.util.request({
            url: "entry/wxapp/DelCollect",
            cachetime: "0",
            data: {
                id: scid,
            },
            success: function(res) {
                let list = ths.data.collect_list,
                    newlist = [];
                for (var i = 0; i < list.length; i++) {
                    if (list[i].id != scid) {
                        newlist.push(list[i]);
                    }
                }
                ths.setData({
                    isDel: -1,
                    collect_list: newlist
                })
                wx.showToast({
                    title: "取消收藏成功",
                    duration: 1e3,
                    icon: "success"
                });

            }
        });
    },
    // 跳转商家详情
    tzsjxq: function(t) {
        1 == t.currentTarget.dataset.type;
        wx.navigateTo({
            url: "/zh_cjdianc/pages/takeout/takeoutindex?storeid=" + t.currentTarget.dataset.sjid
        });
    },
    // 下拉刷新
    onPullDownRefresh: function() {
        this.setData({
            page: 1,
            mygd: !1,
            jzgd: !0,
            loadmore: true,
            isDel: -1,
            is_onPullDown: true
        })
        this.getCollectList();
    },
    // 到达底部加载更多
    onReachBottom: function() {
        if (!this.data.loadmore) return;
        this.setData({
            isDel: -1,
            jzgd: !1
        })
        this.getCollectList();
    },
    //手指刚放到屏幕触发
    touchS: function(e) {
        //判断是否只有一个触摸点
        if (e.touches.length == 1) {
            this.setData({
                //记录触摸起始位置的X坐标
                startX: e.touches[0].clientX
            });
        }
    },
    //触摸时触发，手指在屏幕上每移动一次，触发一次
    touchM: function(e) {
        var that = this
        if (e.touches.length == 1) {
            //记录触摸点位置的X坐标
            var moveX = e.touches[0].clientX;
            //计算手指起始点的X坐标与当前触摸点的X坐标的差值
            var disX = that.data.startX - moveX;
            //delBtnWidth 为右侧按钮区域的宽度
            if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变
                this.setData({
                    isDel: -1
                })
            } else if (disX > 55) { //移动距离大于0，文本层left值等于手指移动距离
                this.setData({
                    isDel: e.currentTarget.dataset.indexs
                })
            }
            var index = e.currentTarget.dataset.indexs;
        }
    },
});